package sl.com.sl;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.BufferedImage;

import javax.swing.JEditorPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.BoxView;
import javax.swing.text.Position;
import javax.swing.text.View;

import sl.com.sl.data.CSExchange;
import sl.com.sl.data.HighlightInfo;
import ar.com.visualAORE.view.ZoomControl;

public class ImageCreator {
	public static BufferedImage createStripesImage(CSExchange data) {

		StackTraceElement[] trace = Thread.currentThread().getStackTrace();
		if (trace.length < 2) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					throw new IllegalArgumentException();
				}
			});
			return null;
		}

		if (!"getDemoImage".equals(Thread.currentThread().getStackTrace()[2]
				.getMethodName())) {
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					throw new IllegalArgumentException();
				}
			});
			return null;
		}

		JEditorPane edit = data.getEditor();
		BufferedImage res = new BufferedImage(edit.getWidth(),
				edit.getHeight(), 2);
		/*
		 * int width = (int)(edit.getWidth()*0.30); int height =
		 * (int)(edit.getHeight()*0.30); BufferedImage res = new
		 * BufferedImage(width, height, 2);
		 */

		Graphics2D g2d = (Graphics2D) res.getGraphics();
		g2d.setColor(edit.getBackground());
		g2d.fillRect(0, 0, edit.getWidth(), edit.getHeight());
		// g2d.fillRect(0, 0, width, height);
		g2d.setColor(Color.black);
		g2d.drawRect(0, 0, edit.getWidth() - 1, edit.getHeight() - 1);
		// g2d.drawRect(0, 0, width - 1, height - 1);

		HighlightInfo[] highlights = data.getHighlights();
		for (int i = 0; i < highlights.length; i++) {
			HighlightInfo hi = highlights[i];
			g2d.setColor(hi.getColor());

			int pos = hi.getStartOffset();
			while (pos < hi.getEndOffset()) {
				System.out.println("POS: " + pos);
				View leaf = getLeafViewForPosition(pos, edit);
				if (leaf == null)
					break;
				try {
					// No cambiar
					int start = Math.max(pos, leaf.getStartOffset());
					int end = Math.min(hi.getEndOffset(), leaf.getEndOffset());

					Rectangle vShape = getAllocation(leaf, edit).getBounds();

					Shape startShape = leaf.modelToView(start, vShape,
							Position.Bias.Forward);
					Shape endShape = leaf.modelToView(end, vShape,
							Position.Bias.Forward);

					vShape.x = startShape.getBounds().x;
					vShape.width = (endShape.getBounds().x
							+ endShape.getBounds().width - vShape.x);

					g2d.fill(vShape);
					pos = leaf.getEndOffset();
				} catch (BadLocationException e) {
					e.printStackTrace();
				}

			}
			System.out.println("Imagen terminada");
		}
		g2d.dispose();
		// return res;
		// Aplico un zoomOut 3
		return ZoomControl.zoomOut(res, 3);
	}

	public static Shape getAllocation(View v, JEditorPane edit) {
		Insets ins = edit.getInsets();
		View vParent = v.getParent();
		int x = ins.left;
		int y = ins.top;

		while (vParent != null) {
			int i = vParent.getViewIndex(v.getStartOffset(),
					Position.Bias.Forward);
			// Tamano de la imagen
			Shape alloc = vParent.getChildAllocation(i, new Rectangle(0, 0,
					32767, 32767));

			x += alloc.getBounds().x;
			y += alloc.getBounds().y;

			vParent = vParent.getParent();
		}

		if ((v instanceof BoxView)) {
			int ind = v.getParent().getViewIndex(v.getStartOffset(),
					Position.Bias.Forward);
			Rectangle r2 = v.getParent().getChildAllocation(ind,
					new Rectangle(0, 0, 2147483647, 2147483647)).getBounds();

			return new Rectangle(x, y, r2.width, r2.height);
		}

		return new Rectangle(x, y, (int) v.getPreferredSpan(0), (int) v
				.getPreferredSpan(1));
	}

	public static View getLeafViewForPosition(int pos, JEditorPane edit) {
		View v = edit.getUI().getRootView(edit);

		while ((v != null) && (v.getViewCount() > 0)) {
			int i = v.getViewIndex(pos, Position.Bias.Forward);
			v = v.getView(i);
		}
		return v;
	}
}
